package com.yash.rms.filter;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.web.authentication.WebAuthenticationDetails;
import org.springframework.stereotype.Repository;


@Repository(value="authenticationAuditFilter")
public class AuthenticationAuditFilter implements ApplicationListener<AuthenticationSuccessEvent>{

	private @Autowired HttpServletRequest request;
	
	@Override
	public void onApplicationEvent(AuthenticationSuccessEvent event) {
		System.out.println(event.getAuthentication().getName());
		System.out.println(event.getTimestamp());
		User user = (User)  event.getAuthentication().getPrincipal();
		System.out.println(user.getUsername());
		WebAuthenticationDetails details = (WebAuthenticationDetails) event.getAuthentication().getDetails();
		System.out.println(details.getRemoteAddress());
		System.out.println(details.getSessionId());
		
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	
	
}
